﻿Public Class Russell

    Private ver As New verificacion
    Dim costo As Integer = 0
    Dim fil, col As Integer
    Dim cont As Boolean = True 'bandera determina si existe panel disponible
    Public Sub juguemosRussell()

        ver.limpiar()
        While cont = True
            calcularuv()
            costo = -1 'inicializo en -1 por si existen costos cero
            For i = 1 To grupbox1.empresas
                For j = 1 To grupbox1.bodegas
                    'buscar mayor costo y almacenar ij
                    For Each Control As Control In grupbox1.splitContainer1.Panel1.Controls
                        If Control.Name = "Panel" & i & j And Control.Enabled = True Then
                            'busco el panel que tenga la mayor penalizacion
                            If Val(Control.Controls.Item(0).Text) > costo And Control.Enabled = True Then
                                costo = Val(Control.Controls.Item(0).Text)
                                fil = i
                                col = j
                                'si la penalizacion es igual a la actual mayor
                            ElseIf Val(Control.Controls.Item(0).Text) = costo And Control.Enabled = True Then
                                For Each elemento As Control In grupbox1.splitContainer1.Panel1.Controls
                                    If elemento.Name = "Panel" & fil & col And elemento.Enabled = True Then
                                        'verifico cual tiene el costo menor 
                                        '( o igual en el caso de que todos los costos sean iguales)
                                        If Val(elemento.Controls.Item(2).Text) >= Val(Control.Controls.Item(2).Text) Then
                                            costo = Val(Control.Controls.Item(0).Text)
                                            fil = i
                                            col = j
                                        End If
                                    End If
                                Next
                            End If
                        End If
                    Next
                Next
            Next
            'buscar capacidades del panel fil,col
            For Each u As Control In grupbox1.splitContainer1.Panel1.Controls
                If u.Name = "UpDown" & fil & 1 Then
                    For Each v As Control In grupbox1.splitContainer1.Panel1.Controls
                        If v.Name = "UpDow" & 1 & col Then
                            For Each Control As Control In grupbox1.splitContainer1.Panel1.Controls
                                If Control.Name = "Panel" & fil & col Then
                                    'asignar segun capacidades
                                    If Val(u.Text) > Val(v.Text) Then
                                        Control.Controls.Item(1).Text = Val(v.Text)
                                        u.Text = Val(u.Text) - Val(v.Text)
                                        v.Text = 0
                                        'eliminar los paneles de la columna
                                        For i = 1 To grupbox1.empresas
                                            For Each panel As Control In grupbox1.splitContainer1.Panel1.Controls
                                                If panel.Name = "Panel" & i & col Then
                                                    panel.Enabled = False
                                                End If
                                            Next
                                        Next
                                    Else
                                        Control.Controls.Item(1).Text = Val(u.Text)
                                        v.Text = Val(v.Text) - Val(u.Text)
                                        u.Text = 0
                                        'eliminar los paneles de la fila
                                        For i = 1 To grupbox1.bodegas
                                            For Each panel As Control In grupbox1.splitContainer1.Panel1.Controls
                                                If panel.Name = "Panel" & fil & i Then
                                                    panel.Enabled = False
                                                End If
                                            Next
                                        Next
                                    End If
                                End If
                            Next
                        End If
                    Next
                End If
            Next
            cont = False
            For i = 1 To grupbox1.empresas
                For j = 1 To grupbox1.bodegas
                    'ver si aun quedan paneles disponibles
                    For Each Control As Control In grupbox1.splitContainer1.Panel1.Controls
                        If Control.Name = "Panel" & i & j Then
                            If Control.Enabled = True Then
                                cont = True
                            End If
                        End If
                    Next
                Next
            Next
        End While

        For i = 1 To grupbox1.empresas
            For j = 1 To grupbox1.bodegas
                For Each Control As Control In grupbox1.splitContainer1.Panel1.Controls
                    If Control.Name = "Panel" & i & j Then
                        Control.Enabled = True
                        Control.Controls.Item(0).Text = ""
                    End If
                Next
            Next
        Next
        PanelAMatriz()
    End Sub
    Public Sub calcularuv()
        Dim i, j As Integer
        ver.limpiar()
        For i = 1 To grupbox1.empresas
            costo = -1
            'buscar el mayor costo por fila
            For j = 1 To grupbox1.bodegas
                For Each Control As Control In grupbox1.splitContainer1.Panel1.Controls
                    If Control.Name = "Panel" & i & j And Control.Enabled = True Then
                        If Val(Control.Controls.Item(2).Text) > costo Then
                            costo = Val(Control.Controls.Item(2).Text)
                        End If
                    End If
                Next
            Next
            'asignar al label el mayor costo
            For Each Control As Control In grupbox1.Controls
                If Control.Name = "u" & i Then
                    Control.Text = costo
                End If
            Next
        Next
        For i = 1 To grupbox1.bodegas
            costo = -1
            'buscar mayor costo por columna
            For j = 1 To grupbox1.empresas
                For Each Control As Control In grupbox1.splitContainer1.Panel1.Controls
                    If Control.Name = "Panel" & j & i And Control.Enabled = True Then
                        If Val(Control.Controls.Item(2).Text) > costo Then
                            costo = Val(Control.Controls.Item(2).Text)
                        End If
                    End If
                Next
            Next
            'asignar valor a label
            For Each Control As Control In grupbox1.Controls
                If Control.Name = "v" & i Then
                    Control.Text = costo
                End If
            Next
        Next
        'en el area de verificacion asignar ui+vj-costo
        For i = 1 To grupbox1.empresas
            For j = 1 To grupbox1.bodegas
                For Each u As Control In grupbox1.Controls
                    If u.Name = "u" & i Then
                        For Each v As Control In grupbox1.Controls
                            If v.Name = "v" & j Then
                                For Each Control As Control In grupbox1.splitContainer1.Panel1.Controls
                                    If Control.Name = "Panel" & i & j And Control.Enabled = True Then
                                        Control.Controls.Item(0).Text = Val(u.Text) + Val(v.Text) - Val(Control.Controls.Item(2).Text)
                                    End If
                                Next
                            End If
                        Next
                    End If
                Next
            Next
        Next

    End Sub
End Class
